const {ipcRenderer} = require('electron');
const last_time = document.getElementById("last_time");

let downTime_Hour;
let downTime_Minue;
let downTime_Sleep_Hour;
let downTime_Sleep_Minue;

let time_30_down = 0;

//获取鼠标按下时的位置
let getWindowBeforePageX;
//获取鼠标松开时的位置
let getWindowafterPageX;

let time_h_buffer;
let time_m_buffer;
let time_s_buffer;

window.addEventListener('DOMContentLoaded',async()=>{
    ipcRenderer.send('new-win-hour');
    ipcRenderer.on('new-win-returnHour',async(event,data)=>{
        downTime_Hour =await data;
        console.log('hour:',data)
    })

    ipcRenderer.send('new-win-minue');
    ipcRenderer.on('new-win-returnMinue',async(event,data)=>{
        downTime_Minue = await data;
        console.log('minue:',data)
    })
    ipcRenderer.send('newWin-no-top')
    ipcRenderer.send('new-win-sleeptime-h')
    ipcRenderer.on('new-win-sleep-returnHour',async(event,data)=>{
        downTime_Sleep_Hour = await data;
        console.log('minue:',data)
    })
    ipcRenderer.send('new-win-sleeptime-m')
    ipcRenderer.on('new-win-sleep-returnMinue',async(event,data)=>{
        downTime_Sleep_Minue = await data;
        console.log('minue:',data)
    })
})

let isDY30;
let time_run = setInterval(async()=>{
    let time_now = new Date();
    let time_now_h = time_now.getHours();
    let time_now_m = time_now.getMinutes();
    let time_now_s = time_now.getSeconds();

    // console.log(downTime_Minue)
    
    if(downTime_Minue == 60){
        time_h_buffer = downTime_Hour - time_now_h
        time_m_buffer = 60 - time_now_m - 1;
    }else if(downTime_Minue == 30){
        
        if(time_now_m >= downTime_Minue){
            time_h_buffer = downTime_Hour - time_now_h - 1
            if(time_30_down >= 30){
                time_30_down = 0;
            }
            // time_m_buffer = 60 - time_30_down++;
            
            // setTimeout(()=>{
            //     time_30_down += 1;
            // },60*1000)
            time_m_buffer = 59 - time_now_m +30;
            
        }else{
            time_h_buffer = downTime_Hour - time_now_h
            time_m_buffer = 30 - time_now_m - 1;
        }
    }else{
        ipcRenderer.send('time-wait')
    }
    
    //休息时间
    if(time_now_h == downTime_Sleep_Hour && time_now_m == downTime_Sleep_Minue){
        ipcRenderer.send('go-SleepTime',{hour:downTime_Sleep_Hour,minue:downTime_Minue});
    }
    time_s_buffer = 60 - time_now_s - 1
    //console.log(time_now_h,time_now_m,time_now_s)
    // console.log(time_h_buffer,time_m_buffer,time_s_buffer)
    last_time.innerText = `${time_h_buffer}:${time_m_buffer}:${time_s_buffer}`;

    if(time_h_buffer == 0 && time_m_buffer == 0 && time_s_buffer == 0){
        console.log("test-时间到了")
        clearInterval(time_run)
        ipcRenderer.send('time-isUp-new-win')
    }else if(time_h_buffer < 0 || time_m_buffer < 0 || time_s_buffer < 0){
        console.log("test-时间到了")
        clearInterval(time_run)
        ipcRenderer.send('time-error-new-win')
    }

},1000)
let i = 0
const change = async() => {
    const pinnedImage = document.getElementById("pinnedImage");
    console.log(pinnedImage.src,i)
    if(i == 0){
        pinnedImage.style.transform = 'rotate3d(0,0,1,-45deg)';
        ipcRenderer.send('newWin-top')
        i = 1;
    }else{
        // pinnedImage.src = "images/Pinned_xie.svg"
        pinnedImage.style.transform = 'rotate3d(0,0,1,0deg)';
        ipcRenderer.send('newWin-no-top')
        i = 0;
    }
}

window.addEventListener('mousedown',(event)=>{
    if(event.pageX || event.pageY){
        getWindowBeforePageX = event.pageX
        console.log({x:event.pageX,y:event.pageY})
    }
})
//鼠标松开时触发
window.addEventListener('mouseup',(event)=>{
    // let event = window.event || event;
    if(event.pageX || event.pageY){
        getWindowafterPageX = event.pageX
        console.log({x:event.pageX,y:event.pageY})
    }
    if(getWindowBeforePageX - getWindowafterPageX > 0){
        //左拉
        ipcRenderer.send('show-tianqi')
    }
    if(getWindowBeforePageX - getWindowafterPageX < 0){
        //右拉
        ipcRenderer.send('show-learn')
    }
})

const clickClose = async() => {
    ipcRenderer.send('new-win-close');
}

const goToList = () => {
    ipcRenderer.send('go-todolistPage');
}